package com.labmi.experiment;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.MapsId;
import javax.persistence.OneToMany;

@Entity
public class Size {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2127746280099047705L;
	
//	@Id
//	@GeneratedValue
//	private long id;
	
	/**
	 * S, M, L, XL, XXL
	 */
	@Id
	private String sizeName;
	
	/**
	 * 48, 50 etc...
	 */
	private int sizeNumber;
	
	/**
	 * Example: 3 item of size L, 2 item of size M etc..
	 */
	private int itemNumber;
	
	@OneToMany(mappedBy="size", cascade={CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
	private List<ItemStock> itemStockList;


	public Size() {
		
	}
	


	public String getSizeName() {
		return sizeName;
	}


	public void setSizeName(String sizeName) {
		this.sizeName = sizeName;
	}


	public int getSizeNumber() {
		return sizeNumber;
	}


	public void setSizeNumber(int sizeNumber) {
		this.sizeNumber = sizeNumber;
	}


	public List<ItemStock> getItemStockList() {
		if(itemStockList == null)
			itemStockList = new ArrayList<ItemStock>(); //Lazy instantiation
		
		return itemStockList;
	}


	public void setItemList(List<ItemStock> itemStockList) {
		this.itemStockList = itemStockList;
	}


	public void setItemNumber(int itemNumber) {
		this.itemNumber = itemNumber;
	}


	public int getItemNumber() {
		return itemNumber;
	} 
	
	public void addItemStock(ItemStock itemStock) {
		if(itemStock != null)
		{
			if(itemStockList == null)
				itemStockList = new ArrayList<ItemStock>();
			
			this.itemStockList.add(itemStock);
			
			if(itemStock.getSize() != this )			
				itemStock.setSize(this);
		}
			
	}

	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Size)
		{
			return 
				this.sizeName.equals(((Size) obj).getSizeName());
		}
		
		return false;
	}
	
	@Override
	public int hashCode() {
		return sizeName.hashCode();
	}
}
